package com.hk.modules.circuit.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.common.query.QueryGenerator;
import com.hk.common.utils.ResponseUtils;
import com.hk.common.utils.StringUtils;
import com.hk.modules.circuit.pojo.CircuitPojo;
import com.hk.modules.circuit.service.CircuitService;
import com.hk.modules.node.pojo.NodePojo;
import com.hk.modules.node.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2024-04-17
 */
@Controller
@RequestMapping("/circuit")
public class CircuitController {

    @Autowired
    private CircuitService circuitService;

    @Autowired
    private NodeService nodeService;

    @PostMapping("/add")
    public ResponseEntity<Object> addObj(@RequestBody CircuitPojo p){
        p.setCreateTime(new Date()).setUpdateTime(new Date());
        return ResponseUtils.tryReturn("新增信息异常",()-> circuitService.saveCircuit(p));
    }

    @PostMapping("/update")
    public ResponseEntity<Object> updateObj(@RequestBody CircuitPojo p){
        p.setUpdateTime(new Date());
        return ResponseUtils.tryReturn("修改信息异常",()->
                circuitService.updateCircuit(p));
    }

    @PostMapping("/delete")
    public ResponseEntity<Object> deleteObj(@RequestBody CircuitPojo p){
        return ResponseUtils.tryReturn("删除信息异常",()->
                circuitService.removeCircuit(p.getId()));
    }


    @GetMapping("/listPage")
    public ResponseEntity<Object> listPage(@RequestParam(name="current", defaultValue="1") Integer current,
                                           @RequestParam(name="size", defaultValue="10") Integer size,
                                           CircuitPojo obj){
        // 查询

        QueryWrapper<CircuitPojo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(obj.getName())){
            queryWrapper.like("name",obj.getName());
            QueryWrapper<NodePojo> nodeQuery = new QueryWrapper<>();
            nodeQuery.like("name",obj.getName());
            nodeQuery.or().like("equip_name",obj.getName());
            nodeQuery.or().like("screen_name",obj.getName());
            List<NodePojo> nodeList = nodeService.list(nodeQuery);
            Set<String> idList = nodeList.stream().map(NodePojo::getCircuitId).collect(Collectors.toSet());
            if(idList.size()>0){
                queryWrapper.or().in("id",idList);
            }
        }
        queryWrapper.orderByAsc("name");
        Page<CircuitPojo> page = new Page<>(current, size);
        return ResponseUtils.tryReturn("查询信息异常",()->circuitService.page(page, queryWrapper));
    }

    @GetMapping("/info")
    public ResponseEntity<Object> getInfo(CircuitPojo p){
        return ResponseUtils.tryReturn("查询信息异常",()->circuitService.getInfo(p.getId()));
    }


    /**
     * 根据端子查询
     * @param p
     * @return
     */
    @GetMapping("/listTree")
    public ResponseEntity<Object> listTree(NodePojo p){
        return ResponseUtils.tryReturn("查询信息异常",()->circuitService.listTree(p));
    }


}

